package com.icarsaas.saas.tenant.cars.customer.web;

import com.icarsaas.saas.common.page.SoulPage;
import com.icarsaas.saas.common.utils.ResultVoUtil;
import com.icarsaas.saas.common.vo.ResultVo;
import com.icarsaas.saas.component.actionLog.action.SaveAction;
import com.icarsaas.saas.component.actionLog.action.StatusAction;
import com.icarsaas.saas.component.actionLog.annotation.ActionLog;
import com.icarsaas.saas.component.actionLog.annotation.EntityParam;
import com.icarsaas.saas.tenant.cars.customer.model.Customer;
import com.icarsaas.saas.tenant.cars.customer.service.CustomerService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @title: MemberController
 * @description: 会员管理
 * @author xrong120@gmail.com
 * @date 2021年1月23日
 */
@Controller @Api(tags="客户信息模块") @RequestMapping(value = "/cars/customer")
public class CutomerController {
	@Autowired private CustomerService customerService;
	
	@GetMapping("/customerList") //@RequiresPermissions("system:dict:status")
	public String memberList() {
		return "/customer/customerList";
	}

	@PostMapping("listSoulTable") @ResponseBody
	public Object listSoulTable(SoulPage<Map<String, Object>> page, @RequestBody Map<String, Object> params) {
		page.setParams(params, page);
		page = customerService.listSoulData(page, params);
		return page;
	}

	@ModelAttribute
	public Customer get(@RequestParam(required = false) String id) {
		Customer entity = null;
		if (id != null) entity = customerService.get(id);
		else entity = new Customer();
		return entity;
	}
	/**
	 * 跳转到编辑页面
	 */
	@GetMapping({"customerForm", "customerForm/{id}"})
	public String customerForm(@RequestParam(value = "id", required = false) Customer entity, Model model) {
		if (entity != null) model.addAttribute("entity", entity);
		return "/customer/customerForm";
	}
	/**
	 * 保存添加/修改的数据
	 */
	@PostMapping({"/saveEntity"}) @ResponseBody
	@ActionLog(name = "客户管理", message = "客户信息：${name}", action = SaveAction.class)
	public ResultVo<?> saveEntity(@EntityParam Customer entity) {
		customerService.save(entity);
		return ResultVoUtil.SAVE_SUCCESS;
	}

	/**
	 * 删除记录
	 * @param ids 删除的数据id
	 * @param param 状态标识，必须带
	 * @return
	 */
	@PostMapping("/deleteEntity") @ResponseBody
	@ActionLog(name = "客户管理", message = "删除客户信息：${name}", action = StatusAction.class)
	public ResultVo<?> deleteEntity(@RequestParam(value = "ids", required = false) List<String> ids
			, @RequestParam(value = "param", required = false) String param) {
		// 更新状态
		if (ids != null && ids.size() > 0) {
			ids.forEach(id -> {
				customerService.delete(id);
			});
			return ResultVoUtil.success("操作成功");
		} else {
			return ResultVoUtil.error("操作失败，请重试!");
		}
	}

	/**
	 * 跳转到详细页面
	 */
	@GetMapping("/detail/{id}")
	public String toDetail(@PathVariable("id") Customer entity, Model model){
		model.addAttribute("entity", entity);
		return "/customer/customerDetail";
	}

}
